﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Linq;
using ChurchData.Business;

namespace ChurchData
{
    public partial class LKTableMaintenanceForm : Form
    {
        #region Public Properties
        public string GridColumnText { get; set; }
        public string HeaderText { get; set; }
        public string LabelText { get; set; }

        #endregion Public Properties

        #region Private Properties
        private int _PK;
        private SaveType FormSaveType = SaveType.None;
        private ILKTable oLK;
        private LKRelationshipBO rlBO = new LKRelationshipBO();

        #endregion Private Properties

        #region Constructor & Form Load
        public LKTableMaintenanceForm(ILKTable lkTable)
        {
            oLK = lkTable;

            InitializeComponent();

            PopulateGrid();

            this.grpEdit.Enabled = false;
        }

        /// <summary>
        /// Used for the LKRelationship table
        /// </summary>
        public LKTableMaintenanceForm()
        {
            InitializeComponent();

            PopulateGrid();

            this.grpEdit.Enabled = false;
            this.txtDescriptionTwo.Visible = true;
        }

        private void LKTableMaintenanceForm_Load(object sender, EventArgs e)
        {
            this.Text = this.HeaderText;
            this.lbDescriptionText.Text = LabelText;
            this.grdResults.Columns["Description"].HeaderText = GridColumnText;
        }

        #endregion Constructor & Form Load

        #region Populate Methods
        private void PopulateGrid()
        {
            if (oLK != null)
            {
                List<GenericLKTableResults> oList = oLK.GetActiveRecords();
                this.grdResults.DataSource = oList;
                this.grdResults.Columns["PK"].Visible = false;
                this.grdResults.Columns["DescriptionTwo"].Visible = false;
            }
            else
            {
                List<GenericLKTableResults> oList = rlBO.GetActiveRecords();
                this.grdResults.DataSource = oList;
                this.grdResults.Columns["PK"].Visible = false;
            }
        }

        #endregion Populate Methods

        #region Control Events
        private void btnClose_Click(object sender, EventArgs e)
        {
           this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.FormSaveType = SaveType.Add;
            this.lblSavedResult.Visible = false;
            this.lblSavedResult.Visible = false;
            this.txtDescriptionText.Text = "";
            this.txtDescriptionTwo.Text = "";
            this.grpEdit.Enabled = true;
            this.txtDescriptionText.Focus();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if(this.grdResults.SelectedRows.Count == 0)
            {
                MessageBox.Show("A row needs to be selected to modify.");    
                return;
            }

            this._PK = (int)this.grdResults.SelectedRows[0].Cells["PK"].Value;
            this.txtDescriptionText.Text = this.grdResults.SelectedRows[0].Cells["Description"].Value.ToString();
            if (oLK == null)
                this.txtDescriptionTwo.Text = this.grdResults.SelectedRows[0].Cells["DescriptionTwo"].Value.ToString();

            this.lblSavedResult.Visible = false;
            this.grpEdit.Enabled = true;

            this.FormSaveType = SaveType.Modify;
            this.txtDescriptionText.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.FormSaveType == SaveType.Add)
            {
                AddRecord();
            }
            else if (this.FormSaveType == SaveType.Modify)
            {
                ModifyRecord();
            }
        }

        private void grdResults_DoubleClick(object sender, EventArgs e)
        {
            this.btnModify.PerformClick();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.grdResults.SelectedRows.Count == 0)
            {
                MessageBox.Show("A row needs to be selected to delete.");    
                return;
            }

            if (DialogResult.OK == MessageBox.Show("Are you sure you want to delete it", "Confirm Delete", MessageBoxButtons.OKCancel))
            {

                this._PK = (int)this.grdResults.SelectedRows[0].Cells["PK"].Value;
                if (oLK != null)
                    oLK.SetToInactive(this._PK);
                else
                    rlBO.SetToInactive(this._PK);

                this.FormSaveType = SaveType.None;
                this.lblSavedResult.Visible = false;
                this.txtDescriptionText.Text = "";
                this.grpEdit.Enabled = false;

                this.PopulateGrid();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.FormSaveType = SaveType.None;
            this.lblSavedResult.Visible = false;
            this.txtDescriptionText.Text = "";
            this.txtDescriptionTwo.Text = "";
            this.grpEdit.Enabled = false;

            this.PopulateGrid();
        }

        private void txtDescriptionText_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) this.btnSave.PerformClick();
        }
        #endregion Control Events

        #region Add/Modify methods
        private void AddRecord()
        {
            if (!Valid()) return;

            if (oLK != null)
                oLK.InsertRecord(this.txtDescriptionText.Text);
            else
                rlBO.InsertRecord(this.txtDescriptionText.Text.Trim(), this.txtDescriptionTwo.Text.Trim());

            this.lblSavedResult.Text = "Saved!";
            this.lblSavedResult.Visible = true;

            this.FormSaveType = SaveType.None;
            this.txtDescriptionText.Text = "";
            this.txtDescriptionTwo.Text = "";
            this.grpEdit.Enabled = false;

            this.PopulateGrid();
        }

        private void ModifyRecord()
        {
            if (!Valid()) return;

            if (oLK != null)
                oLK.ModifyRecord(this._PK, this.txtDescriptionText.Text.Trim());
            else
                rlBO.ModifyRecord(this._PK, this.txtDescriptionText.Text.Trim(), txtDescriptionTwo.Text.Trim());
            this.lblSavedResult.Text = "Saved!";
            this.lblSavedResult.Visible = true;

            this.FormSaveType = SaveType.None;
            this.txtDescriptionText.Text = "";
            this.grpEdit.Enabled = false;

            this.PopulateGrid();
        }

        private bool Valid()
        {
            bool isValid = true;
            errorProvider1.Clear();
            errorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            if (txtDescriptionText.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtDescriptionText, "Required");
                isValid = false;
            }

            if (oLK == null)
            {
                if (txtDescriptionTwo.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtDescriptionTwo, "Required");
                    isValid = false;
                }
            }

            return isValid;
        }
        #endregion Add/Modify methods
    }//class
}//namespace
